/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.ejb.facade.actions;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ipo.mm.ejb.entities.Actions;
import org.ipo.mm.ejb.entities.Meeting;

/**
 *
 * @author makcro
 */
@Stateless
public class ActionsFacade implements ActionsFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Actions actions) {
        em.persist(actions);
    }

    public void edit(Actions actions) {
        em.merge(actions);
    }

    public void remove(Actions actions) {
        em.remove(em.merge(actions));
    }

    public Actions find(Object id) {
        return em.find(Actions.class, id);
    }

    public List<Actions> findAll() {
        return em.createQuery("select object(o) from Actions as o").getResultList();
    }

    public void create(String name, String text, String status, Date dueDate, Meeting meeting){

        Actions action = new Actions();
        action.setActionText(text);
        action.setDeleted(false);
        action.setDueDate(dueDate);
        action.setMeetingId(meeting);
        action.setName(name);
        action.setStatus(status);

        create(action);
        

    }

    public Long getCount(){
        return (Long) em.createNamedQuery("Actions.findAllNotDeletedCount").getSingleResult();
    }

    public List<Actions> findAllNotDeleted(int firstResult, int maxResults){


        return (List<Actions>) em.createNamedQuery("Actions.findAllNotDeleted")
                .setFirstResult(firstResult)
                .setMaxResults(maxResults)
                .getResultList();
    }

    public List<Actions> findByStatusNotDeleted(String status, int firstResult, int maxResults){
        return (List<Actions>) em.createNamedQuery("Actions.findByStatusNotDeleted")
                .setParameter("status", status)
                .setFirstResult(firstResult)
                .setMaxResults(maxResults)
                .getResultList();
    }

    public Long getByStatusNotDeletedCount(String status){
        return (Long) em.createNamedQuery("Actions.findByStatusNotDeletedCount").setParameter("status", status).getSingleResult();
    }


}
